package com.grp6.game.ui.platform.text.ansi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.grp6.game.ui.platform.IDialog;
import com.grp6.game.ui.platform.IQuestionValidator;

public class AnsiCharacterDialog implements IDialog {
	private ArrayList<QuestionStruct> questionaire = new ArrayList<QuestionStruct>();
	private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	private ColorPrinter out;
	public AnsiCharacterDialog(ColorPrinter printer) {
		this.out = printer;
	}

	@Override
	public void addQuestions(String question, IQuestionValidator validator) {
		QuestionStruct struct = new QuestionStruct();
		struct.question = question;
		struct.validator = validator;
		questionaire.add(struct);
	}

	@Override
	public void display() {
		for(QuestionStruct q: questionaire)
			do{
				out.println(q.question);
				try {
					q.answer = in.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}while(!q.validator.isValid(q.question, q.answer));
	}

	private class QuestionStruct{
		String question;
		String answer;
		IQuestionValidator validator;
	}
}
